<template>
  <div class="app-container">
    <!-- 搜索框 -->
    <div class="search">
      <el-form ref="search" :inline="true" :model="search">
        <!-- 合作商搜索 -->
        <el-form-item label="合作商搜索:" size="medium">
          <el-input v-model="search.name" clearable placeholder="请输入" />
        </el-form-item>
        <el-button
          type="primary"
          class="my-primary1"
          icon="el-icon-search"
          @click="getPartnerList"
        >查询</el-button>
      </el-form>
    </div>

    <!-- 表单区域 -->
    <div class="result">
      <!-- 头部 -->
      <el-row class="operation">
        <el-button
          type="primary"
          class="my-primary2"
          icon="el-icon-circle-plus-outline"
          @click="addFormFn"
        >新建
        </el-button>
      </el-row>
      <!-- 表单主体 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column
          type="index"
          :index="(index) => (search.pageIndex - 1) * 10 + (index + 1)"
          label="序号"
          width="80"
        />
        <el-table-column prop="name" label="合作商名称" />
        <el-table-column prop="account" label="账号" />
        <el-table-column prop="vmCount" label="设备数量" />
        <el-table-column prop="ratio" label="分成比例(%)" />
        <el-table-column prop="contact" label="联系人" />
        <el-table-column prop="mobile" label="联系电话" />
        <el-table-column label="操作" width="250">
          <template v-slot="{ row }">
            <el-popconfirm
              title="确定要重置合作商密码吗？"
              @onConfirm="resetPassword(row.id)"
            >
              <el-button
                slot="reference"
                type="text"
                class="el-button--primary-text"
                style="margin-right:10px"
              >
                重置密码
              </el-button>
            </el-popconfirm>

            <el-button
              type="text"
              class="el-button--primary-text"
              @click="showDetailDialog(row)"
            >
              查看详情
            </el-button>
            <el-button
              type="text"
              class="el-button--primary-text"
              @click="modifyPartner(row.id)"
            >
              修改
            </el-button>
            <el-button
              type="text"
              class="el-button--primary-text del-color"
              @click="delPartner(row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 表单底部 -->
      <div class="pagination-container">
        <el-pagination
          layout="slot,prev,next"
          :current-page.sync="search.pageIndex"
          prev-text="上一页"
          next-text="下一页"
          background
          :total="totalCount"
          :page-size="search.pageSize"
          @current-change="getPartnerList"
        >
          <template order="-1">
            <span
              class="el-pagination__total"
            >共{{ totalCount }}条记录 第 {{ search.pageIndex }}/{{
              totalPage
            }}
              页</span>
          </template>
        </el-pagination>
      </div>
    </div>
    <!-- 新增合作商弹窗 -->
    <el-dialog
      :title="`${currentId ? '修改' : '新增'}合作商`"
      :visible.sync="addFormDialog"
      width="630px"
      @close="closeFn"
    >
      <el-form
        ref="addForm"
        :model="addForm"
        :rules="rules"
        label-width="140px"
      >
        <el-form-item label="合作商名称：" prop="name">
          <el-input
            v-model="addForm.name"
            autocomplete="off"
            placeholder="请输入"
            style="width: 396px"
            show-word-limit
            maxlength="15"
          />
        </el-form-item>
        <el-form-item label="联系人：" prop="contact">
          <el-input
            v-model="addForm.contact"
            autocomplete="off"
            placeholder="请输入"
            style="width: 396px"
            show-word-limit
            maxlength="15"
          />
        </el-form-item>
        <el-form-item label="联系电话：" prop="mobile">
          <el-input
            v-model="addForm.mobile"
            autocomplete="off"
            placeholder="请输入"
            style="width: 396px"
            show-word-limit
            maxlength="15"
          />
        </el-form-item>
        <el-form-item label="分成比例(%)：" prop="ratio">
          <el-input-number
            v-model="addForm.ratio"
            controls-position="right"
            :min="0"
            :max="100"
            placeholder="请输入"
            style="width: 396px"
          />
        </el-form-item>
        <el-form-item v-if="!currentId" label="账号：" prop="account">
          <el-input
            v-model="addForm.account"
            autocomplete="off"
            placeholder="请输入"
            style="width: 396px"
            show-word-limit
            maxlength="15"
          />
        </el-form-item>
        <el-form-item v-if="!currentId" label="密码：" prop="password">
          <el-input
            v-model="addForm.password"
            autocomplete="off"
            placeholder="请输入"
            style="width: 396px"
            show-word-limit
            maxlength="15"
          />
        </el-form-item>
        <div class="dialog-footer">
          <el-row type="flex" align="middle" justify="center">
            <el-col style="text-align: center">
              <el-button
                class="my-secondary"
                @click="addFormDialog = false"
              >取 消</el-button>
              <el-button
                type="primary"
                class="my-primary2"
                style="margin-left: 34px"
                @click="saveData"
              >确 定</el-button>
            </el-col>
          </el-row>
        </div>
      </el-form>
    </el-dialog>
    <!-- 查看详情 -->
    <el-dialog title="合作商详情" :visible.sync="detailDialog" width="630px">
      <el-form :model="partnerInfo" label-width="140px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="合作商名称：">{{
              partnerInfo.name
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人：">{{
              partnerInfo.contact
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话：">{{
              partnerInfo.mobile
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分成比例：">
              <template>
                {{ partnerInfo.ratio + "%" }}
              </template>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {
  getPartnerList,
  addPartner,
  modifyPartner,
  delPartner,
  resetPassword
} from '@/api/partner'
export default {
  name: 'PartnerContainer',
  data() {
    return {
      search: {
        pageIndex: 1,
        pageSize: 10,
        name: ''
      },
      addFormDialog: false,
      detailDialog: false,
      addForm: {
        name: '',
        account: '',
        password: '',
        ratio: 0,
        contact: '',
        phone: '',
        mobile: ''
      },
      partnerInfo: [],
      partnerList: [],
      tableData: [],
      totalPage: 0,
      totalCount: 0,
      currentId: null,
      rules: {
        name: [{ required: true, message: '请输入', trigger: 'change' }],
        account: [{ required: true, message: '请输入', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getPartnerList()
  },
  methods: {
    // 查看详情
    showDetailDialog(row) {
      this.detailDialog = true
      console.log(row)
      this.partnerInfo = row
    },
    // 查找合作商
    async getPartnerList() {
      const res = await getPartnerList({ ...this.search })
      this.totalCount = +res.totalCount
      this.totalPage = +res.totalPage
      this.tableData = res.currentPageRecords
      // .map(item => {
      //   item.ratio = item.ratio + '%'
      //   return item
      // })
    },
    // 新增
    async addFormFn() {
      this.addFormDialog = true
    },
    // 删除合作商
    async delPartner(id) {
      await delPartner(id)
      this.$message.success('合作商删除成功')
      this.getPartnerList()
    },
    // 重置合作商密码
    async resetPassword(id) {
      // console.log(id)
      await resetPassword(id)
      this.$message.success('重置合作商密码成功')
    },
    // 关闭弹窗(清空表单)
    closeFn() {
      this.currentId = null
      this.addForm = {
        name: '',
        account: '',
        password: '',
        ratio: 0,
        contact: '',
        phone: '',
        mobile: ''
      }
      this.$refs.addForm.resetFields()
      this.addFormDialog = false
    },
    // 修改
    async modifyPartner(id) {
      this.currentId = id
      this.addForm = this.tableData.find((item) => item.id === id)
      this.addFormDialog = true
    },
    // 确认
    async saveData() {
      this.$refs.addForm.validate(async(pass) => {
        if (!pass) return
        this.currentId
          ? await modifyPartner({ ...this.addForm, id: this.currentId })
          : await addPartner({ ...this.addForm })
        this.getPartnerList()
        this.$message.success(`${this.currentId ? '编辑' : '新增'}成功`)
        this.closeFn()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "@/styles/common.scss";
.search .el-form-item {
  margin-bottom: 0;
}

::v-deep .el-form-item__label {
  font-weight: normal;
}

.taskDetail .el-form-item {
  margin-bottom: 0;
}
</style>
